home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: wdl1.wdl.loral.com!lds081!news
- From: John Whitmire <whitmire@lds.loral.com>
- Subject: Re: random number in C++
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <31163A9B.44D0@lds.loral.com>
- Sender: news@lds.loral.com
- Content-Transfer-Encoding: 7bit
- Organization: Loral Data Systems
- References: <4eoh03$6jb@pacemaker.cts> <Pine.SUN.3.91.960201080729.14827F-100000@szechuan>
- Mime-Version: 1.0
- Date: Mon, 5 Feb 1996 17:12:59 GMT
- X-Mailer: Mozilla 2.0b5 (Win16; I)
-
- Joseph Strout wrote:
- >
- > On 31 Jan 1996, Brian A. Bucher wrote:
- >
- > > Is there _ANY_ way to get a truly random number in a C++ program?
- > > rand and srand do not generate random numbers, they generate
- > > pseudo-random numbers, which is what I don't need.
- >
- > There is NO way to get a truly random number in any software. Computers
- > today are deterministic, and given the same starting conditions, any
- > particular algorithm will give you the same result again and again.
- >
- > In principle, one could build a random number chip which seeded itself
- > via quantum fluctuations, environmental noise, etc., but as far as I know
- > nobody has done this.
- >
- > So the best you can do in software is find a good pseudorandom number
- > generator. rand() isn't one -- I remember once I was generating random
- > Turing machines, using only the last bit returned from rand() calls, and
- > they came out 010101010101....
- >
- > random() in the g++ <math.h> library is better, but this isn't available
- > on all platforms. I think "Numerical Recipes in C" discusses
- > pseudorandom number generators, so you could code your own if needed.
- >
- > What most people do is just seed the random number generator with some
- > mostly-unpredictable value, such as the system time or the number of
- > cycles it takes the user to press a key.
- >
- > ,------------------------------------------------------------------.
- > | Joseph J. Strout Department of Neuroscience, UCSD |
- > | jstrout@ucsd.edu http://www-acs.ucsd.edu/~jstrout/ |
- > `------------------------------------------------------------------'
-
- Check out the Algorithm Alley column in DDJ, Nov 94, titled "Truly Random
- Numbers". The author applies some cryptographic principles and some
- external stimuli (your typing) to improve the randomness. Source code is
- available from ftp://ftp.mv.com/pub/ddj. Look for file AAL1194.
-
- --
- =====================================================
- John Whitmire (whitmire@lds.loral.com)
-